define(function (require) {
    var Base = require('group/common/main');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(this, [params]);
            if (_this['page_' + _global.a]) {
                _this['page_' + _global.a]();
            }
        },
        page_direct_buy: function () {
            var _this = this;
            var updateTotalPrice = function () {
                var sel = '#J_total_price';
                var total = _.intval(($(sel).data('val') - _this.$('#J_order_score').val()) * 100) / 100;
                
                $(sel).html("¥" + total);
                $('#J_submit').val(total == 0 ? "提交订单" : "去支付");
            };

            var inputTimeout;
            _this.$('#J_order_score').on('change paste keyup', function () {
                var $this = $(this);
                var score = _.intval($this.val());
                var max = $this.data('max');
                if (score > max) {
                    score = max;
                }
                else if (score <= 0) {
                    score = 0;
                }
                clearTimeout(inputTimeout);
                inputTimeout = setTimeout(function () {
                    $this.val(_.intval(score));
                    updateTotalPrice();
                }, 500);

            });
            updateTotalPrice();
            if (_this.$('input[name=express_id]:checked').size() == 0) {
                _this.$('input[name=express_id]').first().prop('checked', 'checked');
            }
            _this.$('form').validate({
                submitHandler: function (form) {
                    var params = _this.$('form').serializeJson();
                    if (_.empty(params.user_address_id)) {
                        return mask.error('请添加收货地址!', function () {
                            window.location.href = _this.$('.add-address').attr('href');
                        });
                    }
                    mask.show();
                    utils.ajaxPost(_.extend({}, params), function (resp) {
                        mask.hide();
                        window.location.href = resp.data.url;
                    });
                }
            });
        }
    });
});